<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cs6作用域</title>
    <style>
        button {
            margin: 50px;
            width: 100px;
            height: 100px;
        }
    </style>
</head>

<body>
    <button class="box">0</button>
    <button class="box">1</button>
    <button class="box">2</button>
    <script>
        var box = document.querySelectorAll('.box');
        for (let i = 0; i < 3; i++) {
            box[i].addEventListener('click',
                function () {
                    console.log(i);
                }
                ,
                false
            )
        }
    </script>
</body>

</html>